package top.simonby.iron.plugin.data.jpa.support;




import org.springframework.data.jpa.repository.support.SimpleJpaRepository;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * 申明该接口不被spring自动接管
 *
 * @author Simon
 */
public class BaseJpaExtendImpl<T, TD> extends SimpleJpaRepository<T, TD> implements BaseJpaExtend<T, TD> {
    private final EntityManager em;

    public BaseJpaExtendImpl(Class<T> domainClass, EntityManager em) {
        super(domainClass, em);
        this.em = em;
    }

    @Override
    public List findByJpql(String jpql, Object... values) {
        //第一步:创建Query对象
        Query query = em.createQuery(jpql);
        //第二步:把值设置到Query对象中去
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                //因为where的?后面的参数是从1开始的
                query.setParameter(i + 1, values[i]);
            }
        }
        //第三步：返回数据
        return query.getResultList();
    }
}
